{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ca57710c",
   "metadata": {},
   "source": [
    "<a target=\"_blank\" href=\"https://colab.research.google.com/github/cohere-ai/notebooks/blob/main/notebooks/llmu/Prompt_Engineering_Basics.ipynb\">\n",
    "  <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n",
    "</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4fac7165",
   "metadata": {},
   "source": [
    "# Prompt Engineering Basics\n",
    "\n",
    "Prompting is at the heart of working with LLMs. The prompt provides context for the text that we want the model to generate. The prompts we create can be anything from simple instructions to more complex pieces of text, and they are used to encourage the model to produce a specific type of output.\n",
    "\n",
    "Coming up with a good prompt is a bit of both science and art. On the one hand, we know the broad patterns that enable us to construct a prompt that will generate the output that we want. But on the other hand, there is so much room for creativity and imagination.\n",
    "\n",
    "In this notebook, you’ll learn how to craft effective prompts to obtain desirable outputs for various tasks.\n",
    "\n",
    "*Read the accompanying [blog post here](https://docs.cohere.com/docs/prompt-engineering-basics).*"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "834049c9",
   "metadata": {},
   "source": [
    "## Overview"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d7ec57f",
   "metadata": {},
   "source": [
    "The notebook is broken into 5 sections:\n",
    "- **How to Write a Basic Prompt** - Give the model commands with imperative verbs.\n",
    "- **How to Layer Additional Instructions** - Add additional instructions to be more precise about the desired about.\n",
    "- **How to Add Context to a Prompt** - Supply additional information as context to help ground the model's output.\n",
    "- **How to Extract Information** - Retrieve specific information from a larger body of text.\n",
    "- **How to Rewrite Text into Another Format** - Rewrite text (a passage of text) into another format (Q&A list)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0b8bf51",
   "metadata": {},
   "source": [
    "## Setup"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "319754f5",
   "metadata": {},
   "source": [
    "We'll start by installing the tools we'll need and then importing them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a03e9387",
   "metadata": {},
   "outputs": [],
   "source": [
    "! pip install cohere -q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e0895df",
   "metadata": {},
   "outputs": [],
   "source": [
    "#@title Enable text wrapping in Google Colab\n",
    "\n",
    "from IPython.display import HTML, display\n",
    "\n",
    "def set_css():\n",
    "  display(HTML('''\n",
    "  <style>\n",
    "    pre {\n",
    "        white-space: pre-wrap;\n",
    "    }\n",
    "  </style>\n",
    "  '''))\n",
    "get_ipython().events.register('pre_run_cell', set_css)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f1a5060",
   "metadata": {},
   "source": [
    "Fill in your Cohere API key in the next cell. To do this, begin by [signing up to Cohere](https://os.cohere.ai/) (for free!) if you haven't yet. Then get your API key [here](https://dashboard.cohere.com/api-keys)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c465a580",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cohere\n",
    "\n",
    "# Paste your API key here. Remember to not share publicly\n",
    "co = cohere.Client(\"COHERE_API_KEY\") # Insert your Cohere API key"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f0f9a08",
   "metadata": {},
   "source": [
    "Let's also define a function `generate_text()` to take a user message, call the Chat endpoint, and stream the response."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0d4130e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_text(message):\n",
    "    stream = co.chat_stream(message=message)\n",
    "    for event in stream:\n",
    "        if event.event_type == \"text-generation\":\n",
    "            print(event.text, end='')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4d4b1c1",
   "metadata": {},
   "source": [
    "## How to Write a Basic Prompt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "057daf26",
   "metadata": {},
   "source": [
    "The best way to design prompts for a model like [Command](https://cohere.com/models/command) is to give a command or an instruction. One way to do this is by using imperative verbs, for example: generate, write, list, provide, and other variations.\n",
    "\n",
    "For instance, let’s say that we are creating the product description copy for a wireless earbuds product. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3802a5b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Experience the freedom of wireless audio with our state-of-the-art earbuds! Designed to deliver exceptional sound quality and all-day comfort, these earbuds are the perfect companion for your on-the-go lifestyle. Enjoy seamless connectivity, easy controls, and a secure fit, making them your go-to for both work and play."
     ]
    }
   ],
   "source": [
    "generate_text(\"Generate a concise product description for the product: wireless earbuds.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "801c9079",
   "metadata": {},
   "source": [
    "## How to Layer Additional Instructions"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af0ee01b",
   "metadata": {},
   "source": [
    "To be more specific about what we want the output to look like, we need only layer additional instructions in the prompt."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a542f651",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hook: Upgrade your listening experience with the ultimate in wireless freedom! \n",
      "\n",
      "Solution: Introducing our state-of-the-art wireless earbuds, the ultimate companion for audio enthusiasts on the go. Designed to deliver exceptional sound quality and all-day comfort, these earbuds are the perfect solution for hands-free, wire-free listening. \n",
      "\n",
      "Features and Benefits: \n",
      "- Superior Sound: Immerse yourself in crystal-clear, high-definition audio. Our earbuds use advanced drivers for deep bass and crystal-clear trebles, elevating your music, podcast, or call experience. \n",
      "- Secure Fit: Ergonomically designed with multiple tip sizes to ensure a secure and comfortable fit, no matter your ear shape or size. \n",
      "- Long-Lasting Battery: Enjoy up to 24 hours of non-stop listening with a portable charging case, keeping your earbuds powered throughout your day. Quick charge functionality also ensures a fast turnaround for those unexpected moments of low battery. \n",
      "- Seamless Connectivity: Instantly connect to your favorite devices with the latest Bluetooth 5.2 technology, ensuring a stable and reliable connection for uninterrupted listening. \n",
      "\n",
      "Call to Action: Experience the future of wireless audio. Buy now and unlock exceptional sound, anytime, anywhere!"
     ]
    }
   ],
   "source": [
    "generate_text(\"\"\"\n",
    "    Generate a concise product description for the product: wireless earbuds. \n",
    "    Use the following format: Hook, Solution, Features and Benefits, Call to Action.\n",
    "    \"\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42a9a6ae",
   "metadata": {},
   "source": [
    "## How to Add Context to a Prompt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61a9c3fe",
   "metadata": {},
   "source": [
    "The prompt can also be constructed as a combination of an instruction and some context. In the next example, the context is an email."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1b5278a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The email expresses sincere gratitude and appreciation to the team members for their hard work and dedication in organizing a successful community meetup."
     ]
    }
   ],
   "source": [
    "generate_text(\"\"\"\n",
    "    Summarize this email in one sentence.\n",
    "    Dear [Team Members],\n",
    "    I am writing to thank you for your hard work and dedication in organizing our recent community meetup. The event was a great success and it would not have been possible without your efforts.\n",
    "    I am especially grateful for the time and energy you have invested in making this event a reality. Your commitment to ensuring that everything ran smoothly and that our guests had a great time is greatly appreciated.\n",
    "    I am also thankful for the support and guidance you have provided to me throughout the planning process. Your insights and ideas have been invaluable in ensuring that the event was a success.\n",
    "    I am confident that our community will benefit greatly from this event and I am excited to see the positive impact it will have.\n",
    "    Thank you again for your hard work and dedication. I am looking forward to working with you on future events.\n",
    "    Sincerely,\n",
    "    [Your Name]\n",
    "    \"\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70ababe9",
   "metadata": {},
   "source": [
    "This instruction–context prompt format is extremely useful as it means we can supply additional information as context to help ground the model's output. One example is a question-answering system for a company's knowledge base. Given a question (the instruction), the model will only be able to provide accurate answers if provided with the knowledge base (the context)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "611b2e41",
   "metadata": {},
   "source": [
    "## How to Extract Information\n",
    "\n",
    "Let's move to another example—an extraction task, which involves retrieving specific information from a given larger body of text. \n",
    "\n",
    "Given context, which in this case is a description of a movie, we want the model to extract the movie title. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7a671108",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The movie title is \"Deadpool 2\"."
     ]
    }
   ],
   "source": [
    "generate_text(\"\"\"\n",
    "    Extract the movie title from the text below.\n",
    "    Deadpool 2 | Official HD Deadpool's \"Wet on Wet\" Teaser | 2018\n",
    "    \"\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a70f8e69",
   "metadata": {},
   "source": [
    "## How to Rewrite Text into Another Format"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ab2d13a",
   "metadata": {},
   "source": [
    "The model is also effective at tasks that involve taking a piece of text and rewriting it into another format that we need.\n",
    "\n",
    "In the next example, we have a one-line instruction followed by the context, which in this case is a blog excerpt. The instruction is to generate a list of frequently asked questions (FAQ) based on the passage, which involves a mixture of several tasks such as extraction and rewriting. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "83f9631e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Here are some potential FAQs with answers:\n",
      "\n",
      "1. What does the Cohere Platform offer?\n",
      "   - The Cohere Platform provides a simple API that gives developers access to advanced LLMs without requiring expertise in machine learning. The platform also handles the complexities of text data curation, model development, training, and serving, so developers can focus on creating practical applications.\n",
      "\n",
      "2. What are the key language processing capabilities the Cohere Platform provides?\n",
      "   - The Cohere Platform offers two primary language processing capabilities: text generation and text embedding.\n",
      "\n",
      "3. How does text generation work?\n",
      "   - Text generation takes a prompt, such as a few words or a sentence, and generates a stream of text as a response. An example could be receiving an original haiku poem as a completion to the prompt \"Write a haiku.\"\n",
      "\n",
      "4. What is text embedding, and what are its use cases?\n",
      "   - Text embedding takes a piece of text and returns a list of numbers that represent its semantic meaning. This can be applied to various situations, including sentiment analysis, to \"measure\" the text's meaning.\n",
      "\n",
      "5. Do I need machine learning expertise to use the Cohere Platform?\n",
      "   - No, the Cohere Platform is designed to remove the need for machine learning know-how. It simplifies the process, allowing developers to access advanced LLMs easily."
     ]
    }
   ],
   "source": [
    "generate_text(\"\"\"\n",
    "    Given the following text, write down a list of potential frequently asked questions (FAQ), together with the answers.\n",
    "    The Cohere Platform provides an API for developers and organizations to access cutting-edge LLMs without needing machine learning know-how. \n",
    "    The platform handles all the complexities of curating massive amounts of text data, model development, distributed training, model serving, and more. \n",
    "    This means that developers can focus on creating value on the applied side rather than spending time and effort on the capability-building side.\n",
    "    \n",
    "    There are two key types of language processing capabilities that the Cohere Platform provides — text generation and text embedding — and each is served by a different type of model.\n",
    "    \n",
    "    With text generation, we enter a piece of text, or prompt, and get back a stream of text as a completion to the prompt. \n",
    "    One example is asking the model to write a haiku (the prompt) and getting an originally written haiku in return (the completion).\n",
    "    \n",
    "    With text embedding, we enter a piece of text and get back a list of numbers that represents its semantic meaning (we’ll see what “semantic” means in a section below). \n",
    "    This is useful for use cases that involve “measuring” what a passage of text represents, for example, in analyzing its sentiment.\n",
    "    \"\"\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
